<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\Queue;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserAddress;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\models\UserBalance;
use app\models\UserStore;
use app\models\UserStoreLog;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Award
{
    protected $user;
    protected $order;
    protected $set;

    protected $items;

    // 总金额
    protected $amount;

    // 复购商品单价
    protected $price;

    // 复购数量
    protected $box;

    protected $is_bao = false;
    protected $is_repeat = false;

    // 是否为重算订单
    protected $is_reset = false;


    public function __construct(Order $order, $is_reset = false)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $order->user_id]);
        $this->set = SetUserUp::getLastOne();

        $this->is_reset = $is_reset;

        $this->items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->all();

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 复购商品价格
        $this->price = Product::find()
            ->select(['price'])
            ->where(['id' => $product_id])
            ->scalar();

        $this->amount = $this->order->total_pay - $this->order->total_freight;

        // 报单区
        $this->is_bao = ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT']);
        if ($this->is_bao) {
            $this->bao();
            // $this->team();
        }

        // 复购区
        $this->is_repeat = ProductAreaList::checkInArea($product_id, Product::$areaMap['REPEAT']);
        if ($this->is_repeat) {
            $this->order->is_repeat = 1;
            $this->order->save();
            $this->repeat();

            // 记录盒数
            $box = 0;
            foreach ($this->items as $item) {
                $p = SetProductCommission::findOne(['product_id' => $item['product_id']]);
                $box += $p->box * $item['quantity'];
            }
            $this->order->box = $box;
            $this->order->save();
        }

        // 补差价
        $is_bu = ProductAreaList::checkInArea($product_id, Product::$areaMap['BU']);
        if ($is_bu) {
            $this->bu();
        }

    }

    // 补差价
    public function bu()
    {
        $r = Chain::getReferrer($this->user->id, true);
        if (!$r || $r->level < UserLevel::$keys['vip1']) {
            return;
        }

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        $product = SetProductCommission::findOne(['product_id' => $product_id]);
        // var_dump($product->b_vip1);exit;

        $name = '补差价';
        $type = 31;

        $name2 = '差价';
        $type2 = 32;

        if ($r->level == UserLevel::$keys['vip1']) {
            $commission = $product->b_vip1;
        }
        if ($r->level == UserLevel::$keys['vip2']) {
            $commission = $product->b_vip2;
        }
        if ($r->level == UserLevel::$keys['vip3']) {
            $commission = $product->b_vip3;
        }
        if ($r->level == UserLevel::$keys['vip4']) {
            $commission = $product->b_vip4;
        }
        if ($r->level == UserLevel::$keys['vip5']) {
            $commission = $product->b_vip5;
        }

        // var_dump($commission);exit;

        $this->saveCommission($r->id, $commission, $type, $name);

        $has_vip1 = false;
        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;

        $diff_vip1 = $product->b_vip1;
        $diff_vip2 = $product->b_vip2;
        $diff_vip3 = $product->b_vip3;
        $diff_vip4 = $product->b_vip4;
        $diff_vip5 = $product->b_vip5;

        $diff_vip1 -= $commission;
        $diff_vip2 -= $commission;
        $diff_vip3 -= $commission;
        $diff_vip4 -= $commission;
        $diff_vip5 -= $commission;

        // var_dump($diff_vip1, $diff_vip2, $diff_vip3, $diff_vip4, $diff_vip5);exit;

        // 差价收益
        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
        ], true);

        foreach ($chain as $user) {
            if ($user->level <= $this->user->level) {
                continue;
            }

            if ((!$has_vip1 && !$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip1']) {
                // $diff_vip1 = $this->_diff('vip1');
                $this->saveCommission($user->id, $diff_vip1, $type2, $name2);
                $has_vip1 = true;
            }
            if ((!$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip2']) {
                // $diff_vip2 = $this->_diff('vip2');
                if ($has_vip1) {
                    $diff_vip2 -= $diff_vip1;
                }
                $this->saveCommission($user->id, $diff_vip2, $type2, $name2);
                $has_vip2 = true;
            }
            if ((!$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip3']) {
                // $diff_vip3 = $this->_diff('vip3');
                if ($has_vip1) {
                    $diff_vip3 -= $diff_vip1;
                }
                if ($has_vip2) {
                    $diff_vip3 -= $diff_vip2;
                }
                $this->saveCommission($user->id, $diff_vip3, $type2, $name2);
                $has_vip3 = true;
            }
            if ((!$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip4']) {
                // $diff_vip4 = $this->_diff('vip4');
                if ($has_vip1) {
                    $diff_vip4 -= $diff_vip1;
                }
                if ($has_vip2) {
                    $diff_vip4 -= $diff_vip2;
                }
                if ($has_vip3) {
                    $diff_vip4 -= $diff_vip3;
                }
                $this->saveCommission($user->id, $diff_vip4, $type2, $name2);
                $has_vip4 = true;
            }
            if ((!$has_vip5) && $user->level == UserLevel::$keys['vip5']) {
                // $diff_vip5 = $this->_diff('vip5');
                if ($has_vip1) {
                    $diff_vip5 -= $diff_vip1;
                }
                if ($has_vip2) {
                    $diff_vip5 -= $diff_vip2;
                }
                if ($has_vip3) {
                    $diff_vip5 -= $diff_vip3;
                }
                if ($has_vip4) {
                    $diff_vip5 -= $diff_vip4;
                }
                $this->saveCommission($user->id, $diff_vip5, $type2, $name2);
                $has_vip5 = true;
                break;
            }
        }

    }

    // 报单
    public function bao()
    {
        $r = Chain::getReferrer($this->user->id, true);
        if (!$r || $r->level < UserLevel::$keys['vip1']) {
            return;
        }

        $name = '直推';
        $type = 11;

        $name2 = '差价收益';
        $type2 = 12;

        $has_vip1 = false;
        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;

        $diff_vip1 = 0;
        $diff_vip2 = 0;
        $diff_vip3 = 0;
        $diff_vip4 = 0;
        $diff_vip5 = 0;

        // 私董
        if ($this->amount == $this->set->vip5) {
            $commission = 0;
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $this->set->r1_5;
            }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->r2_5;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->r3_5;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->r4_5;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->r5_5;
            }
            $this->saveCommission($r->id, $commission, $type, $name);

            // $has_vip1 = true;
            $diff_vip1 = $this->set->r1_5;
            $diff_vip2 = $this->set->r2_5;
            $diff_vip3 = $this->set->r3_5;
            $diff_vip4 = $this->set->r4_5;
            $diff_vip5 = $this->set->r5_5;
        }

        // 董事
        if ($this->amount == $this->set->vip4) {
            $commission = 0;
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $this->set->r1_4;
            }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->r2_4;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->r3_4;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->r4_4;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->r5_4;
            }
            $this->saveCommission($r->id, $commission, $type, $name);

            // $has_vip2 = true;
            $diff_vip1 = $this->set->r1_4;;
            $diff_vip2 = $this->set->r2_4;;
            $diff_vip3 = $this->set->r3_4;
            $diff_vip4 = $this->set->r4_4;
            $diff_vip5 = $this->set->r5_4;
        }

        // 资源股东
        if ($this->amount == $this->set->vip3) {
            $commission = 0;
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $this->set->r1_3;
            }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->r2_3;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->r3_3;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->r4_3;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->r5_3;
            }
            $this->saveCommission($r->id, $commission, $type, $name);

            // $has_vip3 = true;
            $diff_vip1 = $this->set->r1_3;
            $diff_vip2 = $this->set->r2_3;
            $diff_vip3 = $this->set->r3_3;
            $diff_vip4 = $this->set->r4_3;
            $diff_vip5 = $this->set->r5_3;
        }

        // 联创
        if ($this->amount == $this->set->vip2) {
            $commission = 0;
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $this->set->r1_2;
            }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->r2_2;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->r3_2;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->r4_2;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->r5_2;
            }
            $this->saveCommission($r->id, $commission, $type, $name);

            // $has_vip4 = true;
            $diff_vip1 = $this->set->r1_2;
            $diff_vip2 = $this->set->r2_2;
            $diff_vip3 = $this->set->r3_2;
            $diff_vip4 = $this->set->r4_2;
            $diff_vip5 = $this->set->r5_2;
        }

        // 大使
        if ($this->amount == $this->set->vip1) {
            $commission = 0;
            if ($r->level == UserLevel::$keys['vip1']) {
                $commission = $this->set->r1_1;
            }
            if ($r->level == UserLevel::$keys['vip2']) {
                $commission = $this->set->r2_1;
            }
            if ($r->level == UserLevel::$keys['vip3']) {
                $commission = $this->set->r3_1;
            }
            if ($r->level == UserLevel::$keys['vip4']) {
                $commission = $this->set->r4_1;
            }
            if ($r->level == UserLevel::$keys['vip5']) {
                $commission = $this->set->r5_1;
            }
            $this->saveCommission($r->id, $commission, $type, $name);

            // $has_vip5 = true;
            $diff_vip1 = $this->set->r1_1;
            $diff_vip2 = $this->set->r2_1;
            $diff_vip3 = $this->set->r3_1;
            $diff_vip4 = $this->set->r4_1;
            $diff_vip5 = $this->set->r5_1;
        }

        // 减掉直推
        $diff_vip1 -= $commission;
        $diff_vip2 -= $commission;
        $diff_vip3 -= $commission;
        $diff_vip4 -= $commission;
        $diff_vip5 -= $commission;

        // var_dump($diff_vip1, $diff_vip2, $diff_vip3, $diff_vip4, $diff_vip5);exit;

        // 差价收益
        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
        ], true);

        foreach ($chain as $user) {
            if ($user->level <= $this->user->level) {
                // continue;
                // 因为低推高也有奖
            }

            if ((!$has_vip1 && !$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip1']) {
                // $diff_vip1 = $this->_diff('vip1');
                $this->saveCommission($user->id, $diff_vip1, $type2, $name2);
                $has_vip1 = true;
            }
            if ((!$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip2']) {
                // $diff_vip2 = $this->_diff('vip2');
                if ($has_vip1) {
                    $diff_vip2 -= $diff_vip1;
                }
                $this->saveCommission($user->id, $diff_vip2, $type2, $name2);
                $has_vip2 = true;
            }
            if ((!$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip3']) {
                // $diff_vip3 = $this->_diff('vip3');
                if ($has_vip1) {
                    $diff_vip3 -= $diff_vip1;
                }
                if ($has_vip2) {
                    $diff_vip3 -= $diff_vip2;
                }
                $this->saveCommission($user->id, $diff_vip3, $type2, $name2);
                $has_vip3 = true;
            }
            if ((!$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip4']) {
                // $diff_vip4 = $this->_diff('vip4');
                if ($has_vip1) {
                    $diff_vip4 -= $diff_vip1;
                }
                if ($has_vip2) {
                    $diff_vip4 -= $diff_vip2;
                }
                if ($has_vip3) {
                    $diff_vip4 -= $diff_vip3;
                }
                $this->saveCommission($user->id, $diff_vip4, $type2, $name2);
                $has_vip4 = true;
            }
            if ((!$has_vip5) && $user->level == UserLevel::$keys['vip5']) {
                // $diff_vip5 = $this->_diff('vip5');
                if ($has_vip1) {
                    $diff_vip5 -= $diff_vip1;
                }
                if ($has_vip2) {
                    $diff_vip5 -= $diff_vip2;
                }
                if ($has_vip3) {
                    $diff_vip5 -= $diff_vip3;
                }
                if ($has_vip4) {
                    $diff_vip5 -= $diff_vip4;
                }
                $this->saveCommission($user->id, $diff_vip5, $type2, $name2);
                $has_vip5 = true;
                break;
            }
        }

    }

    // 培育奖, 新加的
    private function _py($user_id)
    {
        $name = '培育奖';
        $type = 222;

        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;

        $diff_vip2 = 0;
        $diff_vip3 = 0;
        $diff_vip4 = 0;
        $diff_vip5 = 0;

        $chain = Chain::getReferrerChain($user_id, [
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
        ], true);

        foreach ($chain as $user) {
            if ((!$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip2']) {
                $diff_vip2 = $this->set->new2;
                $this->saveCommission($user->id, $diff_vip2, $type, $name);
                $has_vip2 = true;
            }
            if ((!$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $this->set->new3;
                $diff_vip3 -= $diff_vip2;
                $this->saveCommission($user->id, $diff_vip3, $type, $name);
                $has_vip3 = true;
            }
            if ((!$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $this->set->new4;
                $diff_vip4 -= $diff_vip3 + $diff_vip2;
                $this->saveCommission($user->id, $diff_vip4, $type, $name);
                $has_vip4 = true;
            }
            if ((!$has_vip5) && $user->level == UserLevel::$keys['vip5']) {
                $diff_vip5 = $this->set->new5;
                $diff_vip5 -= $diff_vip3 + $diff_vip4 + $diff_vip2;
                $this->saveCommission($user->id, $diff_vip5, $type, $name);
                $has_vip5 = true;
                break;
            }
        }
    }


    // 间推, 新加的
    private function _second($user_id)
    {
        $name = '间推';
        $type = 221;

        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;

        $diff_vip3 = 0;
        $diff_vip4 = 0;
        $diff_vip5 = 0;

        $chain = Chain::getReferrerChain($user_id, [
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
        ], true);

        foreach ($chain as $user) {
            if ((!$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $this->set->new_diff3;
                $this->saveCommission($user->id, $diff_vip3, $type, $name);
                $has_vip3 = true;
            }
            if ((!$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $this->set->new_diff4;
                $diff_vip4 -= $diff_vip3;
                $this->saveCommission($user->id, $diff_vip4, $type, $name);
                $has_vip4 = true;
            }
            if ((!$has_vip5) && $user->level == UserLevel::$keys['vip5']) {
                $diff_vip5 = $this->set->new_diff5;
                $diff_vip5 -= $diff_vip3 + $diff_vip4;
                $this->saveCommission($user->id, $diff_vip5, $type, $name);
                $has_vip5 = true;
                break;
            }
        }
    }

    // 获取订单数量
    private function getOrder($user_id, $price)
    {
        $children = Chain::getChildren($user_id);
        $ids = ArrayHelper::getColumn($children, 'id');

        $count = (int) Order::find()
            ->where(['user_id' => $ids])
            ->andWhere(['total_price' => $price])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->count();

        return $count;

        return $count + 1;
    }

    // 平级奖
    public function equal(User $user, $amount)
    {
        $name = '平级奖';
        $type = 20;

        // $chain = Chain::getReferrerChain($user->id, [
        //     $user->level
        // ], true);
        // if ($chain) {
        //     $user = $chain[0];
        //     $this->saveCommission($user->id, $amount, $type, $name);
        // }

        $r = Chain::getReferrer($user->id, true);
        if ($r && $r->level == $user->level) {
            $this->saveCommission($user->id, $amount, $type, $name);
        }
    }

    public function jd(User $user)
    {
        $name = '见单奖';
        $type = 30;

        $chain = Chain::getReferrerChain($user->id, [
            // $user->level,
            // 高级也拿
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
        ], true);
        if ($chain) {
            $user = $chain[0];
            $this->saveCommission($user->id, $this->set->jd, $type, $name);
        }
    }

    // 团队奖
    public function team()
    {
        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
        ], true);

        $name = '团队奖';
        $type = 101;

        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;

        $diff_vip3 = 0;
        $diff_vip4 = 0;
        $diff_vip5 = 0;

        foreach ($chain as $user) {
            if ((!$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $this->_team($this->set->t3);
                $this->saveCommission($user->id, $diff_vip3, $type, $name);
                $has_vip3 = true;
            }
            if ((!$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $this->_team($this->set->t4);
                $diff_vip4 -= $diff_vip3;
                $this->saveCommission($user->id, $diff_vip4, $type, $name);
                $has_vip4 = true;
            }
            if ((!$has_vip5) && $user->level == UserLevel::$keys['vip5']) {
                $diff_vip5 = $this->_team($this->set->t5);
                $diff_vip5 -= $diff_vip4 + $diff_vip3;
                $this->saveCommission($user->id, $diff_vip5, $type, $name);
                $has_vip5 = true;
                break;
            }
        }
    }

    private function _team($ratio)
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        return $amount * $ratio / 100;
    }

    // 我是觉得这个复购是对的, 但下面才是客户要的
    public function repeat__()
    {
        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
        ], true);

        $name = '差价';
        $type = 21;
        $name2 = '平级奖';
        $type2 = 22;

        $has_vip1 = false;
        $has_vip2 = false;
        $has_vip2_2 = false;
        $has_vip3 = false;
        $has_vip3_2 = false;
        $has_vip4 = false;
        $has_vip4_2 = false;
        $has_vip5 = false;
        $has_vip5_2 = false;

        $diff_vip1 = 0;
        $diff_vip2 = 0;
        $diff_vip2_2 = 0;
        $diff_vip3 = 0;
        $diff_vip3_2 = 0;
        $diff_vip4 = 0;
        $diff_vip4_2 = 0;
        $diff_vip5 = 0;
        $diff_vip5_2 = 0;

        foreach ($chain as $user) {
            // if ($user->level <= $this->user->level) {
            // 有平级奖
            if ($user->level < $this->user->level) {
                continue;
            }

            if ((!$has_vip1 && !$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip1']) {
                // vip1 其实没有奖励
                $diff_vip1 = $this->_diff('vip1');
                $this->saveCommission($user->id, $diff_vip1, $type, $name);
                $has_vip1 = true;
            }
            if (((!$has_vip2 || !$has_vip2_2) && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip2']) {
                // 如果是和下单人平级, 就直接返平级奖
                if ($this->user->level == $user->level) {
                    $diff_vip2_2 = $this->_eq('e_2');
                    $this->saveCommission($user->id, $diff_vip2_2, $type2, $name2);
                    $has_vip2 = true;
                    $has_vip2_2 = true;
                } else {
                    if (!$has_vip2 && $diff_vip2 == 0) {
                        $diff_vip2 = $this->_diff('vip2');
                        $diff_vip2 -= $diff_vip1;
                        $this->saveCommission($user->id, $diff_vip2, $type, $name);
                        $has_vip2 = true;
                    } else {
                        $diff_vip2_2 = $this->_eq('e_2');
                        $this->saveCommission($user->id, $diff_vip2_2, $type2, $name2);
                        $has_vip2_2 = true;
                    }
                }

            }
            if (((!$has_vip3 || !$has_vip3_2) && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip3']) {
                // 如果是和下单人平级, 就直接返平级奖
                if ($this->user->level == $user->level) {
                    $diff_vip3_2 = $this->_eq('e_3');
                    $this->saveCommission($user->id, $diff_vip3_2, $type2, $name2);
                    $has_vip3 = true;
                    $has_vip3_2 = true;
                } else {
                    if (!$has_vip3 && $diff_vip3 == 0) {
                        $diff_vip3 = $this->_diff('vip3');
                        $diff_vip3 -= $diff_vip2 + $diff_vip1;
                        $this->saveCommission($user->id, $diff_vip3, $type, $name);
                        $has_vip3 = true;
                    } else {
                        $diff_vip3_2 = $this->_eq('e_3');
                        $this->saveCommission($user->id, $diff_vip3_2, $type2, $name2);
                        $has_vip3_2 = true;
                    }
                }

            }
            if (((!$has_vip4 || !$has_vip4_2) && !$has_vip5) && $user->level == UserLevel::$keys['vip4']) {
                // 如果是和下单人平级, 就直接返平级奖
                if ($this->user->level == $user->level) {
                    $diff_vip4_2 = $this->_eq('e_4');
                    $this->saveCommission($user->id, $diff_vip4_2, $type2, $name2);
                    $has_vip4 = true;
                    $has_vip4_2 = true;
                } else {
                    if (!$has_vip4 && $diff_vip4 == 0) {
                        $diff_vip4 = $this->_diff('vip4');
                        $diff_vip4 -= $diff_vip3 + $diff_vip2 + $diff_vip1;
                        $this->saveCommission($user->id, $diff_vip4, $type, $name);
                        $has_vip4 = true;
                    } else {
                        $diff_vip4_2 = $this->_eq('e_4');
                        $this->saveCommission($user->id, $diff_vip4_2, $type2, $name2);
                        $has_vip4_2 = true;
                    }
                }

            }
            if ((!$has_vip5 || !$has_vip5_2) && $user->level == UserLevel::$keys['vip5']) {

                // 如果是和下单人平级, 就直接返平级奖
                if ($this->user->level == $user->level) {
                    $diff_vip5_2 = $this->_eq('e_5');
                    $this->saveCommission($user->id, $diff_vip5_2, $type2, $name2);
                    $has_vip5 = true;
                    $has_vip5_2 = true;
                    break;
                } else {
                    if (!$has_vip5 && $diff_vip5 == 0) {
                        $diff_vip5 = $this->_diff('vip5');
                        $diff_vip5 -= $diff_vip4 + $diff_vip3 + $diff_vip2 + $diff_vip1;
                        $this->saveCommission($user->id, $diff_vip5, $type, $name);
                        $has_vip5 = true;
                    } else {
                        $diff_vip5_2 = $this->_eq('e_5');
                        $this->saveCommission($user->id, $diff_vip5_2, $type2, $name2);
                        $has_vip5_2 = true;
                        break;
                    }
                }



                // var_dump($has_vip5, $has_vip5_2);exit;
                // break;
            }
        }
    }
    public function repeat()
    {
        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['vip5'],
        ], true);

        $name = '差价';
        $type = 21;
        $name2 = '平级奖';
        $type2 = 22;

        $has_vip1 = false;
        $has_vip2 = false;
        $has_vip3 = false;
        $has_vip4 = false;
        $has_vip5 = false;

        $diff_vip1 = 0;
        $diff_vip2 = 0;
        $diff_vip3 = 0;
        $diff_vip4 = 0;
        $diff_vip5 = 0;

        if ($this->user->level == UserLevel::$keys['vip1']) {
            $e = $this->_eq('e_1');
            $e2 = $this->_eq('e_1_2');
        }
        if ($this->user->level == UserLevel::$keys['vip2']) {
            $e = $this->_eq('e_2');
            $e2 = $this->_eq('e_2_2');
        }
        if ($this->user->level == UserLevel::$keys['vip3']) {
            $e = $this->_eq('e_3');
            $e2 = $this->_eq('e_3_2');
        }
        if ($this->user->level == UserLevel::$keys['vip4']) {
            $e = $this->_eq('e_4');
            $e2 = $this->_eq('e_4_2');
        }
        if ($this->user->level == UserLevel::$keys['vip5']) {
            $e = $this->_eq('e_5');
            $e2 = $this->_eq('e_5_2');
        }

        $r = Chain::getReferrer($this->user->id, true);
        if ($r && $r->level == $this->user->level) {
            $this->saveCommission($r->id, $e, $type2, $name2);

            // 返两级
            $r2 = Chain::getReferrer($r->id, true);
            if ($r2 && $r2->level == $this->user->level) {
                $this->saveCommission($r2->id, $e2, $type2, $name2);
            }
        }

        foreach ($chain as $user) {
            if ($user->level <= $this->user->level) {
            // 有平级奖
            // if ($user->level < $this->user->level) {
            //     continue;
            }

            // if ((!$has_vip1 && !$has_vip2 && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip1']) {
            //     // vip1 其实没有奖励
            //     $diff_vip1 = $this->_diff('vip1');
            //     $this->saveCommission($user->id, $diff_vip1, $type, $name);
            //     $has_vip1 = true;
            // }
            if (((!$has_vip2) && !$has_vip3 && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip2']) {
                $diff_vip2 = $this->_diff('vip2');
                $diff_vip2 -= $diff_vip1;
                $this->saveCommission($user->id, $diff_vip2, $type, $name);
                $has_vip2 = true;

            }
            if (((!$has_vip3) && !$has_vip4 && !$has_vip5) && $user->level == UserLevel::$keys['vip3']) {
                $diff_vip3 = $this->_diff('vip3');
                $diff_vip3 -= $diff_vip2 + $diff_vip1;
                $this->saveCommission($user->id, $diff_vip3, $type, $name);
                $has_vip3 = true;
            }
            if (((!$has_vip4) && !$has_vip5) && $user->level == UserLevel::$keys['vip4']) {
                $diff_vip4 = $this->_diff('vip4');
                $diff_vip4 -= $diff_vip3 + $diff_vip2 + $diff_vip1;
                $this->saveCommission($user->id, $diff_vip4, $type, $name);
                $has_vip4 = true;

            }
            if ((!$has_vip5) && $user->level == UserLevel::$keys['vip5']) {
                $diff_vip5 = $this->_diff('vip5');
                $diff_vip5 -= $diff_vip4 + $diff_vip3 + $diff_vip2 + $diff_vip1;
                $this->saveCommission($user->id, $diff_vip5, $type, $name);
                $has_vip5 = true;
                break;
            }
        }
    }

    // 这个是最新的差价
    protected function _diff($diff_key)
    {
        $order_items = $this->items;

        $sum = 0;
        foreach ($order_items as $item) {
            $product = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();

            // var_dump($product);exit;

            $sum += ($item['price'] - $product[$diff_key]) * $item['quantity'];
        }

        // 用订单价 - 复购价 = 差价
        return $sum;

    }

    protected function _eq($diff_key)
    {
        $order_items = $this->items;

        $sum = 0;
        foreach ($order_items as $item) {
            $product = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();

            $sum += ($product[$diff_key]) * $item['quantity'];
        }

        return $sum;

    }

    // 2 + 1 上级
    private function getIndirect($user_id)
    {
        $the_referrer = Chain::getReferrer($user_id, true);
        if (!$the_referrer) {
            return null;
        }

        // 高等级
        if ($the_referrer->level >= UserLevel::$keys['vip2']) {
            return $the_referrer;
        }

        // 处理两线条
        // if ($the_referrer->level >= UserLevel::$keys['vip2']) {
        //
        //     // 判断是否属于前2个直推
        //     $children = Chain::getChildrenSort($the_referrer->id, ['id']);
        //
        //     // if (count($children) > $this->set->vip2) {
        //     if (count($children) > 2) {
        //         $children = [$children[0], $children[1]];
        //         $in = in_array($user_id, $children);
        //         if (!$in) {
        //             return $the_referrer;
        //         } else {
        //             return $this->getIndirect($the_referrer->id);
        //         }
        //     } else {
        //         return $this->getIndirect($the_referrer->id);
        //     }
        // }
        if ($the_referrer->v1 == 1) {
            return $the_referrer;
        } else {
            return $this->getIndirect($the_referrer->id);
        }

        return $this->getIndirect($the_referrer->id);
    }

    // 保存佣金 (await balance)
    protected function saveCommission($user_id, $balance, $type, $name, $status = 0)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->order->user_id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = $status;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();
            $account = UserAccount::findOne(['user_id' => $user_id]);

            if ($account) {
                if ($status == 1) {
                    $account->balance += $balance;
                } else {
                    $account->await_balance += $balance;
                }

              $account->save();
            }

            // balance logs
            if ($status == 1) {
                UserBalance::addLog($model->user_id,
                    $account->balance, $model->balance, $account->balance + $model->balance, '返佣', $this->order->id);
            }

            return true;
        }
    }

}